<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>

<%--分頁功能 --%>
<%
String p = request.getParameter("p");
if (p == null) {
	session.setAttribute("row", 0);
} else {
	int pages = 0;
	try {
		pages = Integer.parseInt(p);	
	} catch (NumberFormatException e) {
//		e.printStackTrace();
	}
	session.setAttribute("row", (pages)*10);
}
%>
<%--分頁功能 --%>

<%--用javabean取得現在時間再用jstl標籤轉為可用之格式 --%>
<jsp:useBean id="now" class="java.util.Date" scope="session" />
<fmt:formatDate var="today" value="${now }" pattern="yyyy-MM-dd"/>
<fmt:formatDate var="month" value="${now }" pattern="yyyy-MM-01"/>
<%--用javabean取得現在時間再用jstl標籤轉為可用之格式 --%>

<form action="index.jsp?f=4" method="post">
<table>
<caption><h1>查詢出缺勤</h1></caption>
<hr>
<tr><td>員工編號：</td><td>
<c:choose>
	<c:when test="${user_level == 1 }">
		<input type="text" name="user_id" value="${member.user_id }" />
	</c:when>
	<c:otherwise>
		<input type="hidden" name="user_id" value="${member.user_id }" />
		<c:out value="${member.user_id }" />
	</c:otherwise>
</c:choose>
</td></tr>
<tr><td>開始日期：</td><td><input type="text" name="date_begin" value="${month }" class="datepicker" /></td></tr>
<tr><td>結束日期：</td><td><input type="text" name="date_end" value="${today }" class="datepicker" /></td></tr>
<tr><td></td><td><input type="submit" value="查詢" />
<input type="button" value="重新查詢" onclick="self.location.href='index.jsp?f=4'"></td></tr>
</table>
</form>

<%--搜尋單一員工出缺勤資料 --%>
<sql:query var="rs" dataSource="jdbc/j2db" maxRows="10" startRow="${row }">
	SELECT s.user_id
		, s.shift_date
		, wt.wt_name
		, wt.on_time
		, wt.off_time
		, cd.clock_number
		, cd.clock_in
		, cd.clock_out
		, at.attendance_name
	FROM shift s 
		left outer join clockdata cd on s.shift_number = cd.shift_number 
		left outer join leavedata l on l.shift_number = s.shift_number 
		, worktypes wt
		, attendance_type at 
	WHERE s.wt_number = wt.wt_number 
		and s.attendance_status = at.attendance_status 
		and s.user_id = ?
		and (s.shift_date between ? and if(? > now(), now(), ?)) 
	ORDER BY s.shift_date ASC
	<sql:param>${param.user_id }</sql:param>
	<sql:param>${param.date_begin }</sql:param>
	<sql:param>${param.date_end }</sql:param>
	<sql:param>${param.date_end }</sql:param>
</sql:query>
<c:forEach var="r" items="${rs.rows}">
<c:set var="user_id" value="${r.user_id }" />
</c:forEach>
<%--搜尋單一員工出缺勤資料 --%>

<%--計算單一員工出缺勤資料筆數 --%>
<sql:query var="rs2" dataSource="jdbc/j2db" >
	SELECT s.user_id
		, s.shift_date
		, wt.wt_name
		, wt.on_time
		, wt.off_time
		, cd.clock_number
		, cd.clock_in
		, cd.clock_out
		, at.attendance_name
	FROM shift s 
		left outer join clockdata cd on s.shift_number = cd.shift_number 
		left outer join leavedata l on l.shift_number = s.shift_number 
		, worktypes wt
		, attendance_type at 
	WHERE s.wt_number = wt.wt_number 
		and s.attendance_status = at.attendance_status 
		and s.user_id = ?
		and (s.shift_date between ? and if(? > now(), now(), ?)) 
	ORDER BY s.shift_date ASC
	<sql:param>${param.user_id }</sql:param>
	<sql:param>${param.date_begin }</sql:param>
	<sql:param>${param.date_end }</sql:param>
	<sql:param>${param.date_end }</sql:param>
</sql:query>
<%--計算單一員工出缺勤資料筆數 --%>


<br><br>
<c:if test="${user_id != null }">
<table border=1>
	<tr>
		<th>員工編號</th>
		<th>班表日期</th>
		<th>班別</th>
		<th>表定上班時間</th>
		<th>表定下班時間</th>
		<th>上班時間</th>
		<th>下班時間</th>
		<th>出勤狀態</th>
	</tr>
<c:forEach var="r" items="${rs.rows}">
	<tr>
		<td>${r.user_id }</td>
		<td>${r.shift_date }</td>
		<td>${r.wt_name }</td>
		<td>${r.on_time }</td>
		<td>${r.off_time }</td>
		<td>${r.clock_in }</td>
		<td>${r.clock_out  }</td>
		<td>${r.attendance_name }</td>
	</tr>
</c:forEach>
</table>

<fmt:formatNumber var="pages" value="${row/10 }" pattern="#" />
<fmt:formatNumber var="allpages" value="${rs2.rowCount/10 }" pattern="#" />

第
<c:choose>
<c:when test="${allpages > 0}">
<c:out value="${pages+1 }" />&nbsp;
</c:when>
<c:otherwise>
<c:out value="${pages }" />&nbsp;
</c:otherwise>
</c:choose>
/
<c:out value="${allpages }" />
頁&nbsp;
<c:if test="${allpages > 1 }">
	<a href="index.jsp?f=4&user_id=${param.user_id }&date_begin=${param.date_begin }&date_end=${param.date_end }">頁首</a>
</c:if>

<c:if test="${pages < allpages && row != 0}">
	<a href="index.jsp?f=4&user_id=${param.user_id }&date_begin=${param.date_begin }&date_end=${param.date_end }&p=${pages-1 }">上一頁</a>
</c:if>

<c:if test="${pages+1 < allpages && row != null}">
	<a href="index.jsp?f=4&user_id=${param.user_id }&date_begin=${param.date_begin }&date_end=${param.date_end }&p=${pages+1 }">下一頁</a>
</c:if>

<c:if test="${allpages > 1 }">
	<a href="index.jsp?f=4&user_id=${param.user_id }&date_begin=${param.date_begin }&date_end=${param.date_end }&p=${allpages-1}">頁尾</a>
</c:if>
</c:if>